package edu.ubb.scrumster.model.repository;

import java.util.ArrayList;
import java.util.List;

import edu.ubb.scrumster.model.model.AbstractModel;
import edu.ubb.scrumster.model.model.Task;
import edu.ubb.scrumster.model.model.User;
import edu.ubb.scrumster.model.model.UserStory;

public class TaskRepository {

	public TaskRepository() {
	}

	public ArrayList<Task> getAllTask() {
		ArrayList<Task> list = new ArrayList<Task>();
		for (AbstractModel d : DatabaseAccess.getInstance().getAll(DatabaseAccess.TASK)){
			list.add(getTaskById(((Task) d).getId()));
		}
		return list;
	}

	public void addTask(Task g) {
		DatabaseAccess.getInstance().addData(g);
	}

	public void updateTask(Task data) {
		DatabaseAccess.getInstance().updateData(data);
	}

	public void deleteTask(Task data) {
		DatabaseAccess.getInstance().deleteData(data);
	}

	public Task getTaskById(int id) {
		Task t = (Task)DatabaseAccess.getInstance().getDataById(id, DatabaseAccess.TASK);
		t.setUser((User)DatabaseAccess.getInstance().getDataById(t.getUser().getId(), DatabaseAccess.USER));
		t.setUserStory((UserStory)DatabaseAccess.getInstance().getDataById(t.getUserStory().getId(), DatabaseAccess.USERSTORY));
		return t;
	}

	public List<Task> getTaskByUserStory(int id) {
		ArrayList<Task> list = new ArrayList<Task>();
		for (AbstractModel d : DatabaseAccess.getInstance().getTaskByUserStory(id)){
			list.add(getTaskById(((Task) d).getId()));
		}
		return list;
	}
}
